package message

import (
	"context"
	"math"

	"gitee.com/liuxuezhan/ar-platform/admin-api/pkg/code"
	"gitee.com/liuxuezhan/ar-platform/rpc-client/setting/client/message"
	"github.com/zeromicro/go-zero/core/logc"

	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type NotificationMessageListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 通知消息列表
func NewNotificationMessageListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *NotificationMessageListLogic {
	return &NotificationMessageListLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *NotificationMessageListLogic) NotificationMessageList(req *types.NotificationMessageListReq) (resp *types.NotificationMessageListData, err error) {
	u, err := l.svcCtx.SettingService.Message.NotificationMessageList(l.ctx, &message.NotificationMessageListReq{
		Page:       req.Page,
		Size:       req.Size,
		Language:   req.Language, // 语言
		MerchantId: 1,            // 商户ID
		Status:     -1,
	})

	if err != nil {
		logc.Error(l.ctx, "NotificationMessageInfo err:", err)
		return nil, err
	}

	if u.Code != int64(code.SettingRpcSuccessful.Code()) {
		return nil, code.BusinessFailure
	}

	list := make([]types.NotificationMessageListDataItem, 0)
	for _, v := range u.Data.List {
		list = append(list, types.NotificationMessageListDataItem{
			Id:         v.Id,
			MerchantId: v.MerchantId,
			Language:   v.Language,
			Type:       v.Type,
			Title:      v.Title,
			Content:    v.Content,
		})
	}

	return &types.NotificationMessageListData{
		Pagination: types.Pagination{
			Page:      int32(req.Page),
			PageSize:  int32(req.Size),
			Total:     u.Data.Count,
			TotalPage: int64(math.Ceil(float64(u.Data.Count) / float64(req.Size))),
		},
		List: list,
	}, nil
}
